/* Name: KeyInputHandler
 * Author: John Gideon
 * Date: 5-8-12
 * Description: Detects key presses and asynchronously calls the necessary functions
 * Revisions: See Subversion logs at http://code.google.com/p/software-engineering-2012/source/list
 */

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

// http://www.cokeandcode.com/info/tut2d.html
public class KeyInputHandler extends KeyAdapter {
	
	private static Boolean leftPressed = false;
	private static Boolean rightPressed = false;
	private static Boolean upPressed = false;
	private static Boolean downPressed = false;
		
	// Called when a key is pressed
	public void keyPressed(KeyEvent e) {
		switch (e.getKeyCode()) {
			case KeyEvent.VK_F11: // Toggle game in windowed or full screen mode
				if (!Game.isPaused) { Game.triggerToggleFullscreenFlag(); } // Fixes bug where fullscreen doesn't work when paused.
				break;
			case KeyEvent.VK_SPACE: // Move view to Earth
				if (Game.currentRoom == Game.ROOM_GAME) {
					ViewControl.centerToEarth();
				}
				break;
			case KeyEvent.VK_A: // Move view left
				leftPressed = true;
				break;
			case KeyEvent.VK_D: // Move view right
				rightPressed = true;
				break;
			case KeyEvent.VK_W: // Move view up
				upPressed = true;
				break;
			case KeyEvent.VK_S: // Move view down
				downPressed = true;
				break;
			case KeyEvent.VK_M: // Toggle mute
				Sound.toggleMute();
				break;
		}
	} 
		
	// Called when a key is released
	public void keyReleased(KeyEvent e) {			
		switch (e.getKeyCode()) {
			case KeyEvent.VK_A: // Stop moving view left
				leftPressed = false;
				break;
			case KeyEvent.VK_D: // Stop moving view right
				rightPressed = false;
				break;
			case KeyEvent.VK_W: // Stop moving view up
				upPressed = false;
				break;
			case KeyEvent.VK_S: // Stop moving view down
				downPressed = false;
				break;
		}
	}

	// Called when a key is typed
	public void keyTyped(KeyEvent e) {
		// if we hit escape, then quit the game
		if (e.getKeyChar() == 27) {
			Game.quitGame();
		}
	}
	
	// Status return functions
	public static Boolean isLeftPressed() {return leftPressed;}
	public static Boolean isRightPressed() {return rightPressed;}
	public static Boolean isUpPressed() {return upPressed;}
	public static Boolean isDownPressed() {return downPressed;}
	
}